package com.controller;

import com.github.pagehelper.PageInfo;
import com.pojo.Client;

import com.pojo.JdInventory;
import com.pojo.Order;
import com.pojo.Product;
import com.service.ClientService;
import com.service.InventoryService;
import com.service.OrderService;
import com.service.ProductService;
import com.vo.QueryVo;
import com.vo.ResultVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import java.util.List;

@Controller
@RequestMapping ("order")
@ResponseBody
public class OrderController {
    
    @Resource
    private OrderService orderService;
    @Resource
    private ClientService clientService;
    @Resource
    private ProductService productService;
    @Resource
    private InventoryService inventoryService;
    
    @RequestMapping (value = "list", method = RequestMethod.GET)
    public ResultVo<Order> queryByPage(Integer pageNum , Integer pageSize , QueryVo vo) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        PageInfo<Order> distributionPageInfo = orderService.queryByPage(pageNum , pageSize , vo);
        return new ResultVo<>(distributionPageInfo);
    }
    
    // @RequestMapping (value = "{d_num}", method = RequestMethod.DELETE)
    // public ResultVo<Distribution> del(@PathVariable ("d_num") Integer d_num) {
    //     int i = orderService.delByKey(d_num);
    //     if (i == 0) {
    //         return new ResultVo<>(500 , "该信息已被删除！");
    //     }
    //     return new ResultVo<>();
    // }
    
    @RequestMapping (value = "", method = RequestMethod.POST)
    public ResultVo<Order> add(Order order) {
        int i = orderService.add(order);
        if (i == 0) {
            return new ResultVo<>(500 , "注意填写信息！");
        }
        
        //销售后 ，更变库存信息
        JdInventory jdInventory = new JdInventory();
        jdInventory.setP_id(order.getP_id());
        List<JdInventory> inventories = inventoryService.queryByType(jdInventory);
        if (inventories.isEmpty()){
            return new ResultVo<>(500,"仓库无货");
        }
        JdInventory inventory = inventories.get(0);
        inventory.setI_outNum(inventory.getI_outNum() + 1);
        inventory.setI_InventoryNum(inventory.getI_InventoryNum() - 1);
        int update = inventoryService.update(inventory);
        if (update <= 0) {
            return new ResultVo<>(500 , "更改异常！");
        }
        return new ResultVo<>();
    }
    
    @RequestMapping (value = "{o_num}", method = RequestMethod.GET)
    public ResultVo<Order> queryBykey(@PathVariable ("o_num") Integer o_num) {
        Order order = orderService.queryByKey(o_num);
        
        Client client = new Client();
        client.setC_id(order.getC_id());
        List<Client> clients = clientService.queryByType(client);
        if (!clients.isEmpty()) {
            order.setC_name(clients.get(0).getC_name());
        }
        
        return new ResultVo<>(order);
    }
    
    
    @RequestMapping (value = "", method = RequestMethod.PUT)
    public ResultVo<Order> update(Order order) {
        
        //验证信息
        //客户名称
        Client c = new Client();
        Client client = null;
        c.setC_name(order.getC_name());
        List<Client> clients = clientService.queryByType(c);
        if (!clients.isEmpty()) {
            client = clients.get(0);
        } else {
            return new ResultVo<>(500 , "填写名称有误！");
        }
        order.setC_id(client.getC_id());
        
        //商品id
        List<Product> products = productService.queryByType(new Product(order.getP_id()));
        if (products.isEmpty()) {
            return new ResultVo<>(500 , "填写商品编号有误！");
        }
        
        int update = orderService.update(order);
        if (update == 0) {
            return new ResultVo<>(500 , "服务器异常！");
        }
        return new ResultVo<>();
    }
    
    
}
